/* 文本构件示例 t e x t . c */
/* text.c */

 #define GTK_ENABLE_BROKEN

#include <stdio.h>
#include <gtk/gtk.h>

gboolean scroll_handle(GtkWidget *self,GdkEvent *event,gpointer user_data){
	printf("%s:enter!\n",__func__);
	GdkEventScroll *scroll_event = (GdkEventScroll*)event;
	gdouble delta_x,delta_y;
	delta_x = scroll_event->x;
	delta_y = scroll_event->y;

	GtkAdjustment *adj = GTK_TEXT(self)->vadj;
	gdouble max,min,val,page_size,new_val;
	max = adj->upper;
	min = adj->lower;
	val = adj->value;
	page_size = adj->page_size;
	printf("%s:min %f, max %f , val %f , page_size %f\n",__func__,min,max,val,page_size);

	//gdk_event_get_scroll_deltas(event,&delta_x,&delta_y);
	switch(scroll_event->direction){
		case GDK_SCROLL_UP:
			printf("%s:scroll up![%f,%f]\n",__func__,delta_x,delta_y);
			//gtk_adjustment_set_value(adj,value-1);
			new_val = CLAMP(val-5,min,max - page_size);
			adj->value = new_val;
			break;
		case GDK_SCROLL_DOWN:
			//gtk_adjustment_set_value(adj,value+1);
			new_val = CLAMP(val+5,min,max - page_size);
			adj->value = new_val;
			printf("%s:scroll down[%f,%f]\n",__func__,delta_x,delta_y);
			break;
	}
	//gtk_adjustment_value_changed(adj);
	 g_signal_emit_by_name(adj,"changed");
	return FALSE;
}

void text_toggle_editable (GtkWidget *self,GtkWidget *text){
	gtk_text_set_editable(GTK_TEXT(text),GTK_TOGGLE_BUTTON(self)->active);
}

void text_toggle_word_wrap(GtkWidget *self,GtkWidget *text){
	gtk_text_set_word_wrap(GTK_TEXT(text),GTK_TOGGLE_BUTTON(self)->active);
}

void close_application(GtkWidget *self,gpointer data){
	gtk_main_quit();
}

int main(int argc,char **argv){
	GtkWidget *window,*box1,*box2,*hbox,*button,*check,*separator,*table,*vscrollbar,*text;
	GdkColormap *cmap;
	GdkColor color;
	GdkFont *fixed_font;

	FILE *infile;

	gtk_init (&argc, &argv);
	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_widget_set_usize (window, 600, 500);
	gtk_window_set_policy (GTK_WINDOW(window), TRUE, TRUE, FALSE);
	gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER_ALWAYS);

	gtk_signal_connect (GTK_OBJECT (window), "destroy",
	GTK_SIGNAL_FUNC(close_application),NULL) ;
	gtk_window_set_title (GTK_WINDOW (window), "Text Widget Example");
	gtk_container_set_border_width (GTK_CONTAINER (window), 0);
	box1 = gtk_vbox_new (FALSE, 0);
	gtk_container_add (GTK_CONTAINER (window), box1);
	gtk_widget_show (box1);
	box2 = gtk_vbox_new (FALSE, 10);
	gtk_container_set_border_width(GTK_CONTAINER (box2), 10);
	gtk_box_pack_start(GTK_BOX (box1), box2, TRUE, TRUE, 0);
	gtk_widget_show(box2);
	table = gtk_table_new (2, 2, FALSE);
	gtk_table_set_row_spacing(GTK_TABLE (table), 0, 2);

	gtk_table_set_col_spacing(GTK_TABLE (table), 0, 2);
	gtk_box_pack_start(GTK_BOX (box2), table, TRUE, TRUE, 0);
	gtk_widget_show (table);
	/*创建G t k T e x t构件*/
	text = gtk_text_new(NULL, NULL);
	gtk_text_set_editable(GTK_TEXT (text), TRUE);
	gtk_table_attach(GTK_TABLE (table), text, 0, 1, 0, 1,
	GTK_EXPAND | GTK_SHRINK | GTK_FILL,
	GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
	gtk_widget_show (text);
	/*给G t k T e x t构件添加垂直滚动条 */
	//GtkText *tmp_text = text;
	vscrollbar = gtk_vscrollbar_new(GTK_TEXT(text)->vadj);
	//vscrollbar = gtk_vscrollbar_new(tmp_text->vadjustment);
	gtk_table_attach(GTK_TABLE(table), vscrollbar, 1, 2, 0, 1,
	GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
	gtk_widget_show(vscrollbar);

	gtk_signal_connect(GTK_OBJECT(text),"scroll-event",G_CALLBACK(scroll_handle),NULL);

	/* 取得系统颜色映射,将映射设置为红色 */
	cmap = gdk_colormap_get_system();
	color.red = 0xffff;
	color.green = 0;
	color.blue = 0;
	if (!gdk_color_alloc(cmap, &color)) {
	g_error( " c o u l d n’t a l l o c a t e c o l o r " ) ;
	}
	/* 加载固定字体 */
	fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-300-*-*-*-*-*-*");
	/* 实现文本构件
	* 可以插入一些文本了 */
	gtk_widget_realize (text);
	/*冻结t e x t构件,准备多行更新 */
	gtk_text_freeze (GTK_TEXT (text));
	/* Insert some colored text */
	gtk_text_insert (GTK_TEXT (text), NULL, &text->style->black, NULL,
	"Supports ", -1);
	gtk_text_insert (GTK_TEXT (text), NULL, &color, NULL,
	"colored ", -1);
	gtk_text_insert (GTK_TEXT (text), NULL, &text->style->black, NULL,
	"text and different ", -1);
	gtk_text_insert (GTK_TEXT (text), fixed_font, &text->style->black, NULL,
	"fonts\n\n", -1);
	/* 将t e x t . c文件加载到 t e x t窗口*/
	infile = fopen("text.c", "r");
	if (infile) {
		char buffer[1024];
	int nchars;
	while (1)
	{
	nchars = fread(buffer, 1, 1024, infile);
	gtk_text_insert (GTK_TEXT (text), fixed_font, NULL,
	NULL, buffer, nchars);
	if (nchars < 1024)
		break;
	}
	fclose (infile);
	}
	/* 将t e x t构件"解冻",让变化显示出来 */
	gtk_text_thaw(GTK_TEXT(text));
	hbox = gtk_hbutton_box_new ();
	gtk_box_pack_start (GTK_BOX (box2), hbox, FALSE, FALSE, 0);
	gtk_widget_show (hbox);
	check = gtk_check_button_new_with_label("Editable");
	gtk_box_pack_start (GTK_BOX (hbox), check, FALSE, FALSE, 0);
	gtk_signal_connect (GTK_OBJECT(check), "toggled",
	GTK_SIGNAL_FUNC(text_toggle_editable), text);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), TRUE);
	gtk_widget_show (check);
	check = gtk_check_button_new_with_label("Wrap Words");
	gtk_box_pack_start (GTK_BOX (hbox), check, FALSE, TRUE, 0);
	gtk_signal_connect (GTK_OBJECT(check), "toggled",
	GTK_SIGNAL_FUNC(text_toggle_word_wrap), text);
	gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), FALSE);
	gtk_widget_show (check);
	separator = gtk_hseparator_new ();
	gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
	gtk_widget_show (separator);
	box2 = gtk_vbox_new (FALSE, 10);
	gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
	gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
	gtk_widget_show (box2);
	button = gtk_button_new_with_label ("close");
	gtk_signal_connect (GTK_OBJECT(button), "clicked",
		GTK_SIGNAL_FUNC(close_application),NULL);
	gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);

	GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
	gtk_widget_grab_default (button);
	gtk_widget_show (button);
	gtk_widget_show (window);
	gtk_main ();
	return(0);
}